package com.yingpai.fitness.dvp_2_0.main.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yingpai.fitness.R;

/**
 * =================================================
 * 作者: 国斌
 * 邮箱: remebermelula@aliyun.com
 * 时间: 2018/4/18
 * 类名:  FeedEditTextWithCountRelativeLayout .java
 * 版本: V 1.0
 * 描述:自定义  反馈编辑文本  带有数字提醒
 * ===============================================
 */


public class FeedEditTextWithCountRelativeLayout extends RelativeLayout {

    private RelativeLayout layout = null;//相对布局
    private EditText contentEt = null;//编辑框
    private TextView countTv = null;//最大输入字数
    private int num = 100;//最大输入字数


    private SpannableStringBuilder
            spannableStringBuilder = new SpannableStringBuilder(String.valueOf(num).toString());

    ;

    public FeedEditTextWithCountRelativeLayout(Context context) {
        super(context);
        initWidget(context);
    }

    public FeedEditTextWithCountRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs,
                R.styleable.EditTextWithCount);
        num = array.getInteger(R.styleable.EditTextWithCount_count, 100);

        array.recycle();
        initWidget(context);
    }

    public FeedEditTextWithCountRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initWidget(context);
    }

    /**
     * @return
     */
    public String getInputContent() {
        return contentEt.getText().toString();
    }

    /**
     * 输入内容
     *
     * @param content
     */
    public void setInputContent(String content) {
        contentEt.setText(content);
    }

    /**
     * EditView 设置焦点
     */
    public void setFocusable() {

        contentEt.setFocusable(true);// 在EditView 设置焦点
        contentEt.requestFocus();
    }

    /**
     *
     */
    public void setNoFocusable() {

        contentEt.setFocusable(false);// 在EditView 设置移除焦点
        contentEt.requestFocus();
    }

    public EditText getEditText() {
        return contentEt;
    }

    /**
     * 初始化布局  绑定控件
     *
     * @param context
     */
    public void initWidget(Context context) {
        layout = (RelativeLayout) LayoutInflater.from(context).inflate(
                R.layout.feed_back_input_view, this);

        contentEt = (EditText) layout.findViewById(R.id.conEt);
        contentEt.setPadding(18, 22, 18, 22);
        contentEt.setFocusable(true);
        countTv = (TextView) layout.findViewById(R.id.countTv);
        countTv.setText(0 + "/" + num);

        contentEt.setTextColor(Color.parseColor("#999999"));


        contentEt.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                temp = s;
            }

            public void afterTextChanged(Editable s) {

                if (s.length() == 50) {


                    countTv.setText(s.length() + "/" + spannableStringBuilder.toString().trim());

                } else {

                    spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    countTv.setText(s.length() + "/" + spannableStringBuilder.toString().trim());

                }

                selectionStart = contentEt.getSelectionStart();
                selectionEnd = contentEt.getSelectionEnd();
                if (temp.length() > num) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    contentEt.setText(s);
                    contentEt.setSelection(tempSelection);
                }
            }
        });
    }

}


